package com.flot.yazb.base.rxjava;

import android.util.Log;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Action1;

/**
 * 创建被观察者
 * Created by fengdongfei on 2017/8/25.
 */

public class RxObservableFactory<T> {

    //create方式--
    public Observable<T> createObserAble() {
        Observable observable = Observable.create(new Observable.OnSubscribe<T>() {

            @Override
            public void call(Subscriber<? super T> subscriber) {
                subscriber.onStart();
                subscriber.onNext((T) "Hello");
                subscriber.onNext((T) "Hi");
                subscriber.onNext((T) "Aloha");
                subscriber.onCompleted();
                subscriber.unsubscribe();
            }
        });
        return observable;
    }

    //just方式-- 将会依次调用：onNext("Hello");onNext("Aloha");....
    public Observable<T> creeateJust() {
        Observable observable = Observable.just("Hello", "Hi", "Aloha", "kit", "Hello", "Hi", "Aloha", "kit", "Hello", "Hi");
        return observable;
    }

    //from方式-- 将会依次调用：onNext("Hello");onNext("Aloha");....
    public Observable<T> createFrom() {
        String[] words = {"Hello", "Hi", "Aloha"};
        Observable observable = Observable.from(words);
        return observable;
    }

    //--------------------------------------------------------------------------------------------------
    //获取观察者
    public void getRxObServer() {
        Observer<T> ob = new RxObServerFactory<T>().createObservable();
        Subscriber<T> os = new RxObServerFactory<T>().createSubscriber();
    }

    //--------------------------------------------------------------------------------------------------
    public void subAction() {
        //除了 subscribe(Observer) 和 subscribe(Subscriber) ，subscribe() 还支持不完整定义的回调，RxJava 会自动根据定义创建出 Subscriber
        Action1<T> onNextAction = new Action1<T>() {
            // onNext()
            @Override
            public void call(T s) {
            }
        };
        Action1<Throwable> onErrorAction = new Action1<Throwable>() {
            // onError()
            @Override
            public void call(Throwable throwable) {
                // Error handling
            }
        };
        Action0 onCompletedAction = new Action0() {
            // onCompleted()
            @Override
            public void call() {
            }
        };
        // 自动创建 Subscriber ，并使用 onNextAction 来定义 onNext()
        new RxObservableFactory<T>().createObserAble().subscribe(onNextAction);
        // 自动创建 Subscriber ，并使用 onNextAction 和 onErrorAction 来定义 onNext() 和 onError()
        new RxObservableFactory<T>().createObserAble().subscribe(onNextAction, onErrorAction);
        // 自动创建 Subscriber ，并使用 onNextAction、 onErrorAction 和 onCompletedAction 来定义 onNext()、 onError() 和 onCompleted()
        new RxObservableFactory<T>().createObserAble().subscribe(onNextAction, onErrorAction, onCompletedAction);
    }

}
